
'''
 1. 先用selenium打开猎聘网
 2. 写入登录名和密码名字，打开滑块验证码，并且获取图片和下载图片
 3. 用opencv将图片变成灰色，将小图片的背景阴影去掉 ，将区域白色更加白，黑色更加黑
 4. 用opencv获取2张图片距离
 5. 根据网站比例获取2张图片X轴的距离
 6. 用selenium拖拽滑块到X轴，注意要分段移动
 7. 失败处理
'''

import time
import cv2
import requests
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from webdriver_manager.core.driver_cache import DriverCacheManager

# 下载驱动
# driver = webdriver.Chrome(service=ChromeService(
#     ChromeDriverManager().install()))

driver = webdriver.Chrome(service=ChromeService(executable_path='C://Users//test//.wdm//drivers//chromedriver//win64//119.0.6045.105//chromedriver-win32//chromedriver.exe'))


driver.maximize_window()

# 打开一个网页
driver.get('https://wow.liepin.com/t1012653/01ab459c.html?mscid=s_00_pz0')

time.sleep(1)

action = ActionChains(driver)

login = driver.find_element(By.XPATH,'//*[@id="login4c-main-container"]/div[2]/div/div[1]/form/div[5]/div')
login.click()

username = driver.find_element(By.XPATH,'//*[@id="login4c-main-container"]/div[2]/div/div[1]/form/div[1]/div/div[1]/div/input')

username.send_keys('1234@qq.com')

password = driver.find_element(By.XPATH,'//*[@id="login4c-main-container"]/div[2]/div/div[1]/form/div[2]/div/div[1]/div/input')
password.send_keys('123456789')

checked = driver.find_element(By.XPATH,'//*[@id="login4c-main-container"]/div[2]/div/div[1]/form/div[3]/label/input')
checked.click()

btnLogin = driver.find_element(By.XPATH,'/html/body/div/div/div[3]/div/div/div[2]/div/div[1]/form/button')
btnLogin.click()

time.sleep(2)

# 进入iframe
driver.switch_to.frame('tcaptcha_iframe')
# 获取图片
min_url =driver.find_element(By.XPATH,'//*[@id="slideBlock"]').get_attribute('src')
bgImg_url =driver.find_element(By.XPATH,'//*[@id="slideBg"]').get_attribute('src')

# 下载图片
with open('./lieping/min.png','wb') as f:
    f.write(requests.get(min_url).content)

with open('./lieping/bgImg.png','wb') as f:
    f.write(requests.get(bgImg_url).content)

# 灰度处理
min = cv2.imread('./lieping/min.png',flags=cv2.IMREAD_GRAYSCALE)
bgImg = cv2.imread('./lieping/bgImg.png',flags=cv2.IMREAD_GRAYSCALE)

# 小图片减去阴影
min = min[23:min.shape[0]-23,23:min.shape[0]-23]

# 超过120就显示黑色，其他显示白色，主要要显示轮廓黑色出来
_,min = cv2.threshold(min,50,255,cv2.THRESH_BINARY_INV)

# 超过100就显示白色，其他显示黑色,主要要显示轮廓黑色出来
_,bgImg = cv2.threshold(bgImg,50,255,cv2.THRESH_BINARY)


# 下载图片查看效果
# cv2.imwrite('./lieping/min_gray.png',min)
# cv2.imwrite('./lieping/bgImg_gray.png',bgImg)

# 黑色轮毂匹配黑色轮毂，有可能识别
match = cv2.matchTemplate(min,bgImg,cv2.TM_CCORR_NORMED)
# 获取数值
x,y = cv2.minMaxLoc(match)[3]
# 获取到X轴
print(x)
# 网站图片比例重新计算X轴,减去默认位置x值
x = x * 341/680-37
print('实际x:',x)

# 拖拽处理
drag = driver.find_element(By.XPATH,'//*[@id="tcaptcha_drag_thumb"]')

# 点击并按住元素
ActionChains(driver).click_and_hold(drag).perform()
# 拖动到x的距离,模拟人的行为拖拽
ActionChains(driver).move_by_offset(xoffset=x/4, yoffset=0).perform()
ActionChains(driver).move_by_offset(xoffset=x/2, yoffset=0).perform()
ActionChains(driver).move_by_offset(xoffset=x/4, yoffset=0).perform()
# 松开鼠标
ActionChains(driver).release(drag).perform()


# 失败处理


# 五秒关闭浏览器
time.sleep(5)
driver.quit()